﻿using Bll;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Web.Models;
using Web.PublicWeb;

namespace Web.Controllers
{
    public class SystemController : Controller
    {
        SysUser sysUser = LoginCookie.GetUser();

        #region User
        public ActionResult UserList()
        {
            BaseModel model = new BaseModel();
            return View(model);
        }

        [HttpPost]
        public string UserQuery(string strWhere, int pageIndex)
        {
            var bll = new TableBll();
            string tblName = "TUser";
            Model_MakePages page = new Model_MakePages(tblName, strWhere, pageIndex);
            PageTable pageTable = bll.GetData_Page_FldsOrder(page);
            string str = Tool.SerializeObject(pageTable);

            return str;
        }
        [HttpPost]
        public ActionResult UserDelete(FormCollection form)
        {
            var bll = new UserBll();
            try
            {
                int id = Convert.ToInt32(form["id"]);
                if (id == 0)
                    return JsonResultExtension.ToJsonResult(false, "操作失败");

                int result = bll.Delete(id);
                if (result > 0)
                    return JsonResultExtension.ToJsonResult(true, "操作成功");
                else
                    return JsonResultExtension.ToJsonResult(false, "操作失败");
            }
            catch (Exception e)
            {
                return JsonResultExtension.ToJsonResult(false, e.Message);
            }
        }

        public ActionResult UserEdit(int id = 0)
        {
            if (id == 0)
            {
                var model = new TUser();
                return View(model);
            }
            else
            {
                var model = new UserBll().GetById(id);
                return View(model);
            }
        }
        [HttpPost]
        public ActionResult UserEdit(TUser model)
        {
            var bll = new UserBll();
            try
            {
                ViewBag.EditResult = "<script>alert('保存失败，请稍后重试！');</script>";
                if (model.TUserId > 0)//修改
                {
                    if (bll.Edit(model) > 0)
                    {
                        ViewBag.EditResult = "<script>alert('保存成功');document.location.href='UserList'</script>";
                    }
                }
                else//新增
                {
                    if (bll.Add(model) > 0)
                    {
                        ViewBag.EditResult = "<script>alert('保存成功');document.location.href='UserList'</script>";
                    }
                }
            }
            catch (Exception exception)
            {
                throw;
            }

            return View(model);
        }
        #endregion

        #region Role
        public ActionResult RoleList()
        {
            BaseModel model = new BaseModel();
            return View(model);
        }

        [HttpPost]
        public string RoleQuery(string strWhere, int pageIndex)
        {
            var bll = new TableBll();
            string tblName = "TRole";
            Model_MakePages page = new Model_MakePages(tblName, strWhere, pageIndex);
            PageTable pageTable = bll.GetData_Page_FldsOrder(page);
            string str = Tool.SerializeObject(pageTable);

            return str;
        }
        [HttpPost]
        public ActionResult RoleDelete(FormCollection form)
        {
            var bll = new RoleBll();
            try
            {
                int id = Convert.ToInt32(form["id"]);
                if (id == 0)
                    return JsonResultExtension.ToJsonResult(false, "操作失败");

                int result = bll.Delete(id);
                if (result > 0)
                    return JsonResultExtension.ToJsonResult(true, "操作成功");
                else
                    return JsonResultExtension.ToJsonResult(false, "操作失败");
            }
            catch (Exception e)
            {
                return JsonResultExtension.ToJsonResult(false, e.Message);
            }
        }

        public ActionResult RoleEdit(int id = 0)
        {
            if (id == 0)
            {
                var model = new TRole();
                return View(model);
            }
            else
            {
                var model = new RoleBll().GetById(id);
                return View(model);
            }
        }
        [HttpPost]
        public ActionResult RoleEdit(TRole model)
        {
            var bll = new RoleBll();
            try
            {
                ViewBag.EditResult = "<script>alert('保存失败，请稍后重试！');</script>";
                if (model.TRoleId > 0)//修改
                {
                    if (bll.Edit(model) > 0)
                    {
                        ViewBag.EditResult = "<script>alert('保存成功');document.location.href='RoleList'</script>";
                    }
                }
                else//新增
                {
                    if (bll.Add(model) > 0)
                    {
                        ViewBag.EditResult = "<script>alert('保存成功');document.location.href='RoleList'</script>";
                    }
                }
            }
            catch (Exception exception)
            {
                throw;
            }

            return View(model);
        }
        #endregion

        #region Company
        public ActionResult CompanyList()
        {
            BaseModel model = new BaseModel();
            return View(model);
        }

        [HttpPost]
        public string CompanyQuery(string strWhere, int pageIndex)
        {
            var bll = new TableBll();
            string tblName = "Company";
            Model_MakePages page = new Model_MakePages(tblName, strWhere, pageIndex);
            PageTable pageTable = bll.GetData_Page_FldsOrder(page);
            string str = Tool.SerializeObject(pageTable);

            return str;
        }
        [HttpPost]
        public ActionResult CompanyDelete(FormCollection form)
        {
            var bll = new CompanyBll();
            try
            {
                int id = Convert.ToInt32(form["id"]);
                if (id == 0)
                    return JsonResultExtension.ToJsonResult(false, "操作失败");

                int result = bll.Delete(id);
                if (result > 0)
                    return JsonResultExtension.ToJsonResult(true, "操作成功");
                else
                    return JsonResultExtension.ToJsonResult(false, "操作失败");
            }
            catch (Exception e)
            {
                return JsonResultExtension.ToJsonResult(false, e.Message);
            }
        }

        public ActionResult CompanyEdit(int id = 0)
        {
            if (id == 0)
            {
                var model = new Company();
                return View(model);
            }
            else
            {
                var model = new CompanyBll().GetById(id);
                return View(model);
            }
        }
        [HttpPost]
        public ActionResult CompanyEdit(Company model)
        {
            var bll = new CompanyBll();
            try
            {
                ViewBag.EditResult = "<script>alert('保存失败，请稍后重试！');</script>";
                if (model.CompanyId > 0)//修改
                {
                    if (bll.Edit(model) > 0)
                    {
                        ViewBag.EditResult = "<script>alert('保存成功');document.location.href='CompanyList'</script>";
                    }
                }
                else//新增
                {
                    if (bll.Add(model) > 0)
                    {
                        ViewBag.EditResult = "<script>alert('保存成功');document.location.href='CompanyList'</script>";
                    }
                }
            }
            catch (Exception exception)
            {
                throw;
            }

            return View(model);
        }
        #endregion

        #region ZhangHu
        public ActionResult ZhangHuList()
        {
            BaseModel model = new BaseModel();
            return View(model);
        }

        [HttpPost]
        public string ZhangHuQuery(string strWhere, int pageIndex)
        {
            var bll = new TableBll();
            string tblName = "ViewZhangHu";
            Model_MakePages page = new Model_MakePages(tblName, strWhere, pageIndex);
            PageTable pageTable = bll.GetData_Page_FldsOrder(page);
            string str = Tool.SerializeObject(pageTable);

            return str;
        }
        //[HttpPost]
        //public ActionResult ZhangHuDelete(FormCollection form)
        //{
        //    var bll = new ZhangHuBll();
        //    try
        //    {
        //        int id = Convert.ToInt32(form["id"]);
        //        if (id == 0)
        //            return JsonResultExtension.ToJsonResult(false, "操作失败");

        //        //int result = bll.Delete(id);
        //        //if (result > 0)
        //        //    return JsonResultExtension.ToJsonResult(true, "操作成功");
        //        //else
        //        //    return JsonResultExtension.ToJsonResult(false, "操作失败");
        //    }
        //    catch (Exception e)
        //    {
        //        return JsonResultExtension.ToJsonResult(false, e.Message);
        //    }
        //}
        //public ActionResult ZhangHuEdit(int id = 0)
        //{
        //    if (id == 0)
        //    {
        //        var model = new ZhangHu();
        //        return View(model);
        //    }
        //    else
        //    {
        //        //var model = new ZhangHuBll().GetById(id);
        //        //return View(model);
        //    }
        //}


        public ActionResult ZhangHuKaiHu()
        {
            return View();
        }
        [HttpPost]
        public ActionResult ZhangHuKaiHu(ZhangHu model)
        {
            var bll = new ZhangHuBll();
            try
            {
                ViewBag.EditResult = "<script>alert('保存失败，请稍后重试！');</script>";

                if (bll.KaiHu(model) > 0)
                {
                    ViewBag.EditResult = "<script>alert('保存成功');document.location.href='CompanyList'</script>";
                }
            }
            catch (Exception exception)
            {
                throw;
            }

            return View(model);
        }

        #endregion

        #region Login
        public ActionResult Login()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Login(TUser model)
        {
            var bll = new UserBll();
            SysUser sysUser = bll.Login(model);
            if (sysUser == null)
            {
                ModelState.AddModelError("alert", "账号密码不匹配");
            }
            else
            {
                LoginCookie.LoginUser(sysUser);
                return RedirectToAction("Default", "Default");
            }

            return View();
        }
        #endregion

    }
}
